//
//  NGStoreNavigationView.m
//  NicoMama
//
//  Created by Deng on 2017/9/13.
//  Copyright © 2017年 NicoMama. All rights reserved.
//

#import "YLStoreNavigationView.h"
 #import "YLAppUtils.h"
 #import "Masonry.h"
#import "UIImage+GIF.h"
 #define kWidthWord 9
@interface YLStoreNavigationView ()

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *shareBtn;
@property (nonatomic, strong) UIView *bottomLine;

@property (nonatomic, assign) CGSize btnSize;
 @property (nonatomic, assign) CGSize titleLabelSize;
 @end

@implementation YLStoreNavigationView

#pragma mark - life cycle

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor =  [UIColor whiteColor];
        [self initViewData];
        [self initViewUI];
        [self layoutMySubviews];
    }
    
    return self;
}

#pragma mark - response
 
- (void)shareBtnDidClick:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(storeNavigationView:didClickedshareBtn:)]) {
        [self.delegate storeNavigationView:self didClickedshareBtn:button];
    }
}

- (void)backBtnDidClick:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(storeNavigationView:didClickBackBtn:)]) {
        [self.delegate storeNavigationView:self didClickBackBtn:button];
    }
}

 

#pragma mark - public method

- (void)updateTitleText:(NSString *)text {
    if (text.length > kWidthWord) {
        NSString *str1 = [text substringToIndex:kWidthWord];
        text = [NSString stringWithFormat:@"%@...",str1];
    }
    self.titleLabel.text = [YLAppUtils filterNull:text];
}


#pragma mark - private method

- (void)initViewData {
    _btnSize = CGSizeMake(40, 40);
     _titleLabelSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 3*_btnSize.width, 24);
}

- (void)initViewUI {
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.backBtn];
     [self.contentView addSubview:self.titleLabel];
     [self.contentView addSubview:self.shareBtn];
      [self.contentView addSubview:self.bottomLine];
 }

- (void)layoutMySubviews {
    @weakify(self);
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.left.top.right.and.bottom.mas_equalTo(self);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.left.bottom.and.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo(0.5f);
    }];
    
    [self.backBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.left.mas_equalTo(self.contentView.mas_left);
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(60, 44));
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-10);
        make.size.mas_equalTo(self.titleLabelSize);
    }];
    
    [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerY.mas_equalTo(self.titleLabel.mas_centerY);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-20);
        make.size.mas_equalTo(self.btnSize);
    }];
    
    
}

#pragma mark - setter and getter

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor clearColor];
    }
    
    return _contentView;
}

- (UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:[UIImage imageNamed:@"icon_nav_back"] forState:UIControlStateNormal];
        CGSize imageSize = [_backBtn imageForState:UIControlStateNormal].size;
        _backBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 16.0f, 0, 60 - imageSize.width - 16.0f);
        [_backBtn addTarget:self action:@selector(backBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _backBtn;
}

 

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = [UIColor colorWithHexString:@"#000000"];
        _titleLabel.font = [YLAppUtils ylFontSemibold:16];
        _titleLabel.numberOfLines = 1;
    }
    
    return _titleLabel;
}

- (UIButton *)shareBtn {
    if (!_shareBtn) {
        _shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shareBtn setImage:[UIImage imageNamed:@"iconNavRightShare"] forState:UIControlStateNormal];
        [_shareBtn addTarget:self action:@selector(shareBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _shareBtn;
}

 

- (UIView *)bottomLine {
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] init];
        _bottomLine.backgroundColor = [UIColor colorWithHexString:@""];
        _bottomLine.hidden = YES;// UI不要这个效果，暂时隐藏
    }
    
    return _bottomLine;
}

- (void)setIsHideBackBtn:(BOOL)isHideBackBtn {
    if (_isHideBackBtn == isHideBackBtn) {
        return;
    }
    _isHideBackBtn = isHideBackBtn;
    self.backBtn.hidden = _isHideBackBtn;
}

 
- (void)setStoreNavigationTag:(NGStoreNavigationTag)storeNavigationTag {
    if (_storeNavigationTag == storeNavigationTag) {
        return;
    }
    _storeNavigationTag = storeNavigationTag;
    
    @weakify(self);
     if (_storeNavigationTag == NGStoreNavigationTag_OnlyShareBtn) {
         self.shareBtn.hidden = NO;
        
        [self.shareBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            @strongify(self);
            make.centerY.mas_equalTo(self.titleLabel.mas_centerY);
            make.right.mas_equalTo(self.contentView.mas_right).offset(-20);
            make.size.mas_equalTo(self.btnSize);
        }];
    }else if (_storeNavigationTag == NGStoreNavigationTag_AllNot) {
         self.shareBtn.hidden = YES;
    }
    
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
    [self layoutIfNeeded];
}

- (NSString *)title {
    return self.titleLabel.text;
}

 

@end

